package com.jzr.bedside.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.bedside.common.SecurityHelper;
import com.jzr.bedside.db1.model.TVitalSign;
import com.jzr.bedside.db1.service.TVitalSignService;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.QueryUtil;
import com.jzr.common.util.U;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags="生命体征")
@RequestMapping("/api/vitalSign")
@RestController
public class VitalSignController {
    @Autowired
    private TVitalSignService vitalSignService;

    @ApiOperation("列表")
    @GetMapping("/get")
    public Result<List<TVitalSign>> get(@ApiParam("病人编码") @RequestParam String patientCode,
                                         @ApiParam("住院号") @RequestParam String patientCureNo) {
        if (U.isBlank(patientCode)) {
            return Result.fail("病人编码patientCode 不能为空");
        }
        if (U.isBlank(patientCureNo)) {
            return Result.fail("住院号patientCureNo 不能为空");
        }
        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        QueryUtil.orderBy(qw,"op_date desc,op_time");
        qw.eq(TVitalSign.PatientCode_,patientCode)
                .eq(TVitalSign.PatientCureNo_,patientCureNo)
                .isNull(TVitalSign.IsDel_);

        List<TVitalSign> list = vitalSignService.list(qw);
        return Result.success("成功", list, list.size());
    }

    @ApiOperation("save -> 保存")
    @PostMapping("/save")
    public Result<Boolean> save(TVitalSign record) {
        if (U.isBlank(record.getPatientCode())) {
            return Result.fail("病人编码patientCode 不能为空");
        }
        if (U.isBlank(record.getPatientCureNo())) {
            return Result.fail("住院号patientCureNo 不能为空");
        }
        if (U.isBlank(record.getOpDate())) {
            return Result.fail("操作日期不能为空");
        }
        if (U.isBlank(record.getOpTime())) {
            return Result.fail("操作时间不能为空");
        }
        if (!checkOpTime(record.getOpTime())) {
            return Result.fail("操作时间必须是:(2,6,10,14,18,22)");
        }
        int count = vitalSignService.save2(record);
        if (count <= 0) {
            return Result.fail("失败,保存0个记录");
        }
        return Result.success("成功", true);
    }


    @ApiOperation("delete -> 删除")
    @PostMapping("/delete")
    public Result<Boolean> delete(Integer id) {
        if (U.isBlank(id)) {
            return Result.fail("id不能为空");
        }
        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        qw.eq(TVitalSign.Id_,id).isNotNull(TVitalSign.IsDel_);

        TVitalSign record = new TVitalSign();
        Integer userId = SecurityHelper.getUserId();
        record.setUpdateBy(userId);
        record.setUpdateTime(LocalDateTime.now());
        record.setId(id);
        record.setIsDel(true);

        if(!vitalSignService.update(record,qw)){
            return Result.fail("记录不存在");
        }
        return Result.success("成功", true);
    }

    private boolean checkOpTime(int opTime) {
        //2,6,10,14,18,22
        if (opTime != 2 && opTime != 6 && opTime != 10 && opTime != 14 && opTime != 18 && opTime != 22) {
            return false;
        }
        return true;
    }

    @ApiOperation("获取生命体征")
    @GetMapping("/getById")
    public Result<TVitalSign> getById(Integer id){
        TVitalSign TVitalSign = vitalSignService.getById(id);
        return Result.success("成功",TVitalSign);
    }


}